<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$ui = new UI();
$conf = new Config();
$tpl = new Template2();
global $curruser;
if ($curruser['acl'] == 5 && $curruser['percent_sw'] == 1)
{
    err("你是固定占成的大股东，不能走飞！");
}
include( "class/class.Bnowconfig.php" );
$Bnowconfig = new Bnowconfig(0, $curruser['id']);
include( "member/news.php" );
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("<p class=error>帐号已被暂停使用</p>");
    exit();
}
$userlist = GetUserList($curruser['id']);
if ($userlist == "")
{
    $ui->showErrorPage("<p>还没有建立会员！</p>");
    exit();
}
$rs = getdb("select * from lotto where open>='" . date("Y-m-d") . "' order by open");
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>目前没有可以下注期数！</p>");
    exit();
}
$open = $rs->f['open'];
$round = $rs->f['round'];
$lotto_id = $rs->f['id'];
$opnumber = $rs->f['n1'];
$rs->close();
$startmode = "";
$stopbet = $conf->get("waitbet");
if (date("Y-m-d") < $open)
{
    $startmode = "disabled";
}
else if (date("Y-m-d") == $open)
{
    if ($stopbet <= date("Hi") && 0 < $opnumber)
    {
        $startmode = "disabled";
    }
    else if (date("Hi") <= $conf->get("startbet"))
    {
        $startmode = "disabled";
    }
}
$t1 = strtotime($open . " " . $stopbet);
$dt = date("Y-m-d");
$tpl->Set("timestap", $t1 - systime());
$tpl->set("phptime", systime());
$tpl->set("open", $open);
$tpl->set("round", d3($round));
$tpl->set("dt", $dt);
$tpl->set("lotto_id", $lotto_id);
$total_win = array();
$ary = array();
$ary[21] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
$ary[31] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
$ary[41] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
$ary[51] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);

$total_win[21] = 0;
$total_win[31] = 0;
$total_win[41] = 0;
$total_win[51] = 0;

$sql = "select * from bet where lotto_id=" . $lotto_id . " and bettype=36";
$userid = $curruser['id'];
$theacl = $curruser['acl'];
$userlist = GetUserList($userid);
if ($userlist == "")
{
    exit("Not Users!");
    exit();
}
$sql .= " and user_id in (" . $userlist . ") and rame in (0,9)";
$rs = getdb($sql);
while (!$rs->eof)
{
    $percent = 1;
    $sumback = 0;
    if ($theacl == 2)
    {
        $sumback = $rs->f['back1'];
        $percent = $rs->f['mpercent'];
    }
    else if ($theacl == 3)
    {
        $sumback = $rs->f['back2'];
        $percent = $rs->f['percent2'];
    }
    else if ($theacl == 4)
    {
        $sumback = $rs->f['back3'];
        $percent = $rs->f['percent3'];
    }
    else
    {
        $sumback = $rs->f['back4'];
        $percent = $rs->f['percent4'];
    }
    if (0 < $percent)
    {
        $percent /= 100;
    }
    $tmp = $percent * $rs->f['amount'];
    $num = intval(substr($rs->f['numbers'], 0, 2));
    $ary[$num]['amount'] += $tmp;
    ++$ary[$num]['count'];
    $ary[$num]['lost'] += $tmp * $rs->f['rate'];
    $total_win[$num] += $tmp * ( $sumback / 100 );
    $rs->movenext();
}
$rs->close();
if (2 < $theacl)
{
    $sql = "select * from bet2 where lotto_id=" . $lotto_id . " and uperid" . $theacl . "=" . $userid . " and bettype=36 and rame in (0,9)";
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $percent = 1;
        $backs = 100;
        if ($theacl == 255)
        {
            $backs = $rs->f['back5'];
            $percent = $rs->f['percent255'] - 0;
        }
        else if ($theacl == 5)
        {
            $backs = $rs->f['back4'];
            $percent = $rs->f['percent5'];
        }
        else if ($theacl == 4)
        {
            $backs = $rs->f['back3'];
            $percent = $rs->f['percent4'];
        }
        else if ($theacl == 3)
        {
            $backs = $rs->f['back2'];
            $percent = $rs->f['percent3'];
        }
        if (0 < $percent)
        {
            $percent /= 100;
        }
        $tmp = $percent * $rs->f['amount'];
        $num = intval(substr($rs->f['numbers'], 0, 2));
        $ary[$num]['amount'] += $tmp;
        ++$ary[$num]['count'];
        $ary[$num]['lost'] += $tmp * $rs->f['rate'];
        $total_win[$num] += $tmp * ( $backs / 100 );
        $rs->movenext();
    }
    $rs->close();
}
$sql = "select * from bet2 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and bettype=36 and rame in (0,9)";
$rs = getdb($sql);
while (!$rs->eof)
{
    $backs = 100;
    if ($theacl == 5)
    {
        $backs = $rs->f['back5'];
    }
    else if ($theacl == 4)
    {
        $backs = $rs->f['back4'];
    }
    else if ($theacl == 3)
    {
        $backs = $rs->f['back3'];
    }
    else
    {
        $backs = $rs->f['back2'];
    }
    $tmp = $rs->f['amount'];
    $num = intval(substr($rs->f['numbers'], 0, 2));
    $ary[$num]['amount'] -= $tmp;
    $ary[$num]['lost'] -= $tmp * $rs->f['rate'];
    $total_win[$num] -= $tmp * ( $backs / 100 );
    $rs->movenext();
}
$rs->close();
$idisk = 1;
$rs = getdb("select * from rate36 where lotto_id={$lotto_id} and disk={$idisk} order by subid");
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>系统目前维护中，请稍後！</p>");
    exit();
}
$max_rate = array(21 => 0, 31 => 0, 41 => 0, 51 => 0);
while (!$rs->eof)
{
    $subid = $rs->f['subid'];
    $html = "<table width='100' border='1'><tr>";
    $i = 0;
    while ($i <= 9)
    {
        $html .= "<td>" . $i . ")&nbsp;<font color='#FF0000'>" . $rs->f["r" . $i] . "</font></td>";
        if ($i % 3 == 2)
        {
            $html .= "</tr><tr>";
        }
        $max_rate[$subid] = floatval($max_rate[$subid]) < floatval($rs->f["r" . $i]) ? floatval($rs->f["r" . $i]) : floatval($max_rate[$subid]);
        ++$i;
    }
    $html .= "</tr></table>";
    $tpl->set("r36_{$subid}", $html);
    $rs->next();
}
$tpl->set("max_rate_21", $max_rate[21]);
$tpl->set("max_rate_31", $max_rate[31]);
$tpl->set("max_rate_41", $max_rate[41]);
$tpl->set("max_rate_51", $max_rate[51]);
$table_head = "";
$table_body = "";
$i = 0;
while ($i <= 9)
{
    $table_head .= "<td bgColor=\"#5a79c6\" height=\"28\" width=\"5%\" align=\"center\">" . ( "<div align=\"center\"><font color=\"#ffffff\">选择</font></div></td>" ) . "<td bgColor=\"#5a79c6\" height=\"28\" width=\"5%\" align=\"center\">" . "<div align=\"center\"><font color=\"#ffffff\">尾数</font></div></td>";
    $table_body .= "<td bgColor=\"#c0d6eb\" height=\"28\" align=\"center\"><input type=\"checkbox\" name=\"num_{$i}\"></td>" . "<td bgColor=\"#5a79c6\" height=\"28\" align=\"center\"><font color=\"#ffffff\">{$i}</font></td>";
    ++$i;
}
$tpl->set("table_head", $table_head);
$tpl->set("table_body", $table_body);
$tpl->set("b63", "");
$tpl->set("startnowdisable", $startmode);
$tpl->set("money21", $ary[21]['amount']);
$tpl->set("win21", show_numbercolor($total_win[21] - $ary[21]['lost']));
$tpl->set("money31", $ary[31]['amount']);
$tpl->set("win31", show_numbercolor($total_win[31] - $ary[31]['lost']));
$tpl->set("money41", $ary[41]['amount']);
$tpl->set("win41", show_numbercolor($total_win[41] - $ary[41]['lost']));
$tpl->set("money51", $ary[51]['amount']);
$tpl->set("win51", show_numbercolor($total_win[51] - $ary[51]['lost']));
include_once( "class_6h/class.menu.php" );
$fieldArr = array("open" => $open, "lotto_id" => $lotto_id, "split_id" => 1);
$onlineMenu = new menu_6h(12, $fieldArr);
$selectid = getvar("select") == "" ? 0 : getvar("select");
$menuList = $onlineMenu->getMenu($selectid);
$tpl->set("menuList", $menuList);
unset($onlineMenu);
$tpl->load("main", "bnow36.htm");
$tpl->set("jslang", $g_conf['language']);
$tpl->show("main");
$ui->footer();